//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Media.Imaging;
using OpenLS.Core.Serialization;
using OpenLS.Drawing;
using OpenLS.Drawing.Serialization;
using OpenLS.Spreadsheet.Serialization;
using OpenLS.Vml;

namespace OpenLS.Spreadsheet
{
    public class Chartsheet: Sheet
    {
        internal Chartsheet(Workbook workbook, SheetInfo info)
            : base(workbook, info)
        {

        }

        internal override IEnumerable<CustomSheetView> GetCustomSheetViews()
        {
            yield break;
        }

        internal override string UriPrefix
        {
            get { return "/xl/chartsheets/sheet"; }
        }

        internal override string ContentType
        {
            get { return SpreadsheetConstants.ChartsheetContentType; }
        }
        internal override string RelationshipType
        {
            get { return SpreadsheetConstants.ChartsheetRelationshipType; }
        }


        #region IOfficeXmlSerializable Members

        internal override void ReadXmlCore(ReadContext context)
        {
            using (ReadContext c = context.Read("chartsheet"))
            {
                Properties = c.ReadOptionalElement<ChartsheetProperties>("sheetPr");
                using (ReadContext c2 = c.Read("sheetViews"))
                {
                    Views.Clear();
                    while (c2.ReaderLocalName == "sheetView")
                        Views.Add(c.ReadElement<ChartsheetView>());
                }
                Protection = c.ReadOptionalElement<ChartsheetProtection>("sheetProtection");
                _customSheetViews.Clear();
                _customSheetViews.AddRange(c.ReadElements<CustomChartsheetView>("customSheetViews", "customSheetView"));
                PageMargins = c.ReadOptionalElement<PageMargins>("pageMargins");
                PageSetup = c.ReadOptionalElement<ChartsheetPageSetup>(ChartsheetPageSetup.xmlName);
                HeaderFooter = c.ReadOptionalElement<HeaderFooter>("headerFooter");
                if (c.GetData<IShapeReader>() == null)
                    c.AddData<IShapeReader>(new ShapeHelper());
                using (ReadContext c2 = c.Read("drawing"))
                {
                    using (ReadContext rc2 = c2.OpenRelatedContext(c2.GetString(OfficeConstants.RelationshipNamespace + "id"), SpreadsheetConstants.SpreadsheetDrawingNamespaceUri))
                    {
                        Drawing = rc2.ReadElement<Drawing.Drawing>(() => new Drawing.Drawing(null));
                    }
                }
                this.LegacyDrawing = LegacyDrawingHelper.ReadOptionalVmlElementAs(c, "legacyDrawing");
                this.LegacyDrawingHF = LegacyDrawingHelper.ReadOptionalVmlElementAs(c, "legacyDrawingHF");
                this.Background = PictureHelper.ReadOptionalPicture(c);
                this._webPublishItems.Clear();
                _webPublishItems.AddRange(ItemsHelper.ReadItems<WebPublishItem>(c, "webPublishItems"));
                Extension = c.ReadOptionalElement<ExtensionElement>("extLst");
            }
        }

        internal override void WriteXmlCore(WriteContext context)
        {
            using (WriteContext c = context.Write("chartsheet"))
            {
                c.WriteOptionalElement(Properties);
                using (WriteContext c2 = c.Write("sheetViews"))
                {
                    foreach (ChartsheetView s in Views)
                        c2.WriteElement(s);
                }
                c.WriteOptionalElement(Protection);
                if (_customSheetViews.Count > 0)
                {
                    using (var c2 = c.Write("customSheetViews"))
                    {
                        c2.WriteElements(_customSheetViews);
                    }
                }
                c.WriteOptionalElement(PageMargins);
                c.WriteOptionalElement(PageSetup);
                c.WriteOptionalElement(HeaderFooter);
                using (WriteContext c2 = c.Write("drawing"))
                {
                    using (RelatedWriteContext rc2 = c2.CreateRelatedContext (
                        SpreadsheetConstants.SpreadsheetDrawingNamespace,
                        c2.GetAvailableUri("./drawing{0}.xml"),
                        DrawingConstants.DrawingRelationshipType,
                        DrawingConstants.DrawingContentType))
                    {
                        rc2.WriteElement(Drawing);
                        c2.SetString(OfficeConstants.RelationshipNamespace + "id", rc2.Relationship.Id);
                    }
                }
                LegacyDrawingHelper.WriteOptionalVmlElementAs(LegacyDrawing, c, "legacyDrawing");
                LegacyDrawingHelper.WriteOptionalVmlElementAs(LegacyDrawingHF, c, "legacyDrawingHF");
                PictureHelper.WriteOptionalPicture(c, Background);
                ItemsHelper.WriteItems(c, _webPublishItems, "webPublishItems");
                c.WriteOptionalElement(Extension );
            }
        }

        #endregion

            	
        private ChartsheetProperties _sheetPr;

        public ChartsheetProperties Properties
        {
            get { return _sheetPr; }
            set { _sheetPr = value; NotifyPropertyChanged("Properties"); }
        }

        readonly List<ChartsheetView> _sheetViews = new List<ChartsheetView>();
        List<ChartsheetView> Views
        {
            get
            {
                return _sheetViews;
            }
        }
        private ChartsheetProtection _sheetProtection;

        public ChartsheetProtection Protection
        {
            get { return _sheetProtection; }
            set { _sheetProtection = value;
                NotifyPropertyChanged("Protection"); }
        }
        private readonly List<CustomChartsheetView> _customSheetViews = new List<CustomChartsheetView>();
        public List<CustomChartsheetView> CustomViews
        {
            get { return _customSheetViews; }
        }
        private PageMargins _pageMargins;

        public PageMargins PageMargins
        {
            get { return _pageMargins; }
            set { _pageMargins = value; NotifyPropertyChanged("PageMargins"); }
        }
        private ChartsheetPageSetup _pageSetup;

        public ChartsheetPageSetup PageSetup
        {
            get { return _pageSetup; }
            set { _pageSetup = value; NotifyPropertyChanged("PageSetup"); }
        }
        private HeaderFooter _headerFooter;

        public HeaderFooter HeaderFooter
        {
            get { return _headerFooter; }
            set { _headerFooter = value; NotifyPropertyChanged("HeaderFooter"); }
        }
        private Drawing.Drawing _drawing;

        public Drawing.Drawing Drawing
        {
            get { return _drawing; }
            set { _drawing = value; NotifyPropertyChanged("Drawing"); }
        }
        private BitmapSource _picture;

        public BitmapSource Background
        {
            get { return _picture; }
            set { _picture = value; NotifyPropertyChanged("Background"); }
        }
        private ExtensionElement _extLst;
        private VmlDrawing _legacyDrawing;
        private VmlDrawing _legacyDrawingHF;
        private List<WebPublishItem> _webPublishItems = new List<WebPublishItem>();
        internal PropertyChangedEventHandler DimensionPropertyChanged;

        public ExtensionElement Extension
        {
            get { return _extLst; }
            set { _extLst = value; NotifyPropertyChanged("Extension"); }
        }

        public VmlDrawing LegacyDrawingHF
        {
            get { return _legacyDrawingHF; }
            set { _legacyDrawingHF = value;NotifyPropertyChanged("LegacyDrawingHF"); }
        }

        public VmlDrawing LegacyDrawing
        {
            get { return _legacyDrawing; }
            set { _legacyDrawing = value; NotifyPropertyChanged("LegacyDrawing"); }
        }

        internal void NotifyDimensionPropertyChanged (Dimension dim, string propertyName)
        {
            var e = DimensionPropertyChanged;
            if (e != null)
                e(dim, new PropertyChangedEventArgs(propertyName));
        }
    }
}
